﻿package com.flashframeless.gallery.modules.images {
	
	import com.hydrotik.utils.QueueLoader;
	import com.hydrotik.utils.QueueLoaderEvent;
	
	import flash.display.Sprite;
	
	import gs.TweenLite;
	import gs.easing.Quint;
	
	public class ImageItem extends Sprite {
		
		private var _alvo		: Sprite;
		private var _loader		: QueueLoader;
		private var _bigImage	: String;
		private var _background	: Sprite;
		private var _mask		: Sprite;
		
		public function ImageItem() {
			
			instanciate();
			
		}
		
		private function instanciate():void {
			
			_alvo 		= alvoMc;
			_background = bgMc;
			_mask		= maskMc;
			
			setupInteractions();
			
		}
		
		private function setupInteractions():void {
			
			_alvo.alpha = 0;
			
		}
		
		private function onLoadQueueProgress( evt:QueueLoaderEvent ):void {
			
			percentTxt.text = Math.floor( evt.percentage*100 ) + '%';
			
		}
		
		private function onLoadQueueComplete( evt:QueueLoaderEvent ):void {
			
			_loader.removeEventListener( QueueLoaderEvent.QUEUE_COMPLETE, onLoadQueueComplete );
			_loader.removeEventListener( QueueLoaderEvent.QUEUE_PROGRESS, onLoadQueueProgress );
			
			TweenLite.to( percentTxt, .3, { alpha:0 } );
			
			TweenLite.to( _background, .5, { width:_alvo.width, onUpdate:function(){
			
				_mask.width = _background.width;
				parent['parent'].reAlignItens();
			
			} } );
			
			TweenLite.to( _alvo, .3, { alpha:1, ease:Quint.easeOut, onComplete:function() {
				
				percentTxt.visible = false;
				
			} } );
			
		}
		
		public function set image( imageUrl:String ):void {
			
			_loader = new QueueLoader();
			
			_loader.addItem( imageUrl, _alvo, { title:"image" } );
			
			percentTxt.text = '0%';
			
			/*
			_loader.addEventListener( QueueLoaderEvent.ITEM_COMPLETE, 	onLoadItemComplete, 	false, 0, true );
			_loader.addEventListener( QueueLoaderEvent.QUEUE_START, 	onLoadQueueStart, 		false, 0, true );
			*/
			
			_loader.addEventListener( QueueLoaderEvent.QUEUE_PROGRESS, 	onLoadQueueProgress, 	false, 0, true );
			_loader.addEventListener( QueueLoaderEvent.QUEUE_COMPLETE, 	onLoadQueueComplete, 	false, 0, true );
			
			_loader.execute();
			
		}
		
		public function set bigImage( url:String ):void {
		
			_bigImage = url;
		
		}
		
		public function get	bigImage():String{
			
			return _bigImage;
		
		}
		
		
	}
	
}